//
//  WXJSWebViewController.m
//  ParentsCommunity
//
//  Created by tianlong on 2018/8/10.
//  Copyright © 2018年 XES. All rights reserved.
//

#import "WXJSWebViewController.h"
#import "WXWebView.h"
#import "Masonry.h"
#import "xesApp.h"
@interface WXJSWebViewController ()<UIWebViewDelegate,xesAppDelegate>
@property (nonatomic, strong) WXWebView *webView;
@property (nonatomic, strong) WKWebView *wkWeb;
@property (nonatomic, strong) xesApp *xesapp;
@property (nonatomic, strong) NSNumber *lastTranslucent;
@property (nonatomic, strong) NSNumber *curTranslucent;
@property (nonatomic, strong) UIColor *lastbarTintColor;
@property (nonatomic, strong) UIColor *curbarTintColor;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation WXJSWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[WXWebView alloc] init];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.bottom.right.offset(0);
    }];
    
    if(self.url){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
        [self.webView loadRequest:request];
    }
    
    NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:@"index.html"];
    NSString*htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
    
    self.xesapp = [[xesApp alloc] init];
    self.xesapp.delegate = self;
    [self.webView registerJSRuntimeEnvironmentWithObject:self.xesapp];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, 90, 44)];
    [backButton setImage:[UIImage imageNamed:@"homeworkpapertest_nav_back_icon_normal"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"homeworkpapertest_nav_back_icon_normal"] forState:UIControlStateHighlighted];
    [backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [backButton setAutoresizesSubviews:YES];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, -5)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)gotoVC {
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor greenColor];
    vc.title = @"hahahahah";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(!self.curTranslucent) {
        self.curTranslucent = [NSNumber numberWithBool:self.navigationController.navigationBar.translucent];
    }
    else {
        self.navigationController.navigationBar.translucent = self.curTranslucent.boolValue;
    }
    
    if(!self.curbarTintColor) {
        self.curbarTintColor = self.navigationController.navigationBar.barTintColor;
    }
    else {
        self.navigationController.navigationBar.barTintColor = self.curbarTintColor;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if(self.lastTranslucent) {
        self.navigationController.navigationBar.translucent = self.lastTranslucent.boolValue;
    }
    self.navigationController.navigationBar.barTintColor = self.lastbarTintColor;
}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
}

- (void)btnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - xesAppDelegate
- (void)changeNavgationBarTitle:(id)params {
    if ([params isKindOfClass:[NSString class]]) {
        NSString *title = (NSString *)params;
        if (title) {
            self.titleLabel.text = title;
            self.navigationItem.titleView = self.titleLabel;
        }
    }
}

- (void)changeNavgationBarBackgroundColor:(id)params {
    UIColor *color = nil;
    if ([params isKindOfClass:[NSString class]]) {
        color = [UIColor colorWithHexValue:[(NSString *)params integerValue]];
    }
    else {
        color = [UIColor colorWithHexValue:(NSInteger)params];
    }

    //记录原来的
    self.lastTranslucent = [NSNumber numberWithBool:self.navigationController.navigationBar.translucent];
    self.lastbarTintColor = self.navigationController.navigationBar.barTintColor;
    
    //设置现在的
    self.navigationController.navigationBar.translucent = NO;
    self.curTranslucent = [NSNumber numberWithBool:self.navigationController.navigationBar.translucent];

    self.navigationController.navigationBar.barTintColor = color;
    self.curbarTintColor = color;
    
}

- (void)showNavgationBarRightButton:(id)params {
    self.navigationItem.rightBarButtonItem = nil;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"gotoVC" style:UIBarButtonItemStyleDone target:self action:@selector(gotoVC)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)changeTitleColor:(id)params {
    UIColor *titleColor = [UIColor colorWithHexValue:(NSInteger)params];
    if (titleColor) {
        self.titleLabel.textColor = titleColor;
    }
    self.navigationItem.titleView = self.titleLabel;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"request.url=%@",request.URL.absoluteString);
    return YES;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment   = NSTextAlignmentCenter;
        _titleLabel.textColor       = [UIColor blackColor];
        _titleLabel.numberOfLines   = 0;
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}

@end
